package com.yu1996.gulimall.product.service.impl;

import com.yu1996.gulimall.product.entity.AttrEntity;
import com.yu1996.gulimall.product.service.AttrService;
import com.yu1996.gulimall.product.vo.AttrGroupRelationVo;
import com.yu1996.gulimall.product.vo.AttrGroupWithAttrsVo;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu1996.common.utils.PageUtils;
import com.yu1996.common.utils.Query;

import com.yu1996.gulimall.product.dao.AttrGroupDao;
import com.yu1996.gulimall.product.entity.AttrGroupEntity;
import com.yu1996.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements
    AttrGroupService {

  @Autowired
  private AttrService attrService;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<AttrGroupEntity> page = this.page(
        new Query<AttrGroupEntity>().getPage(params),
        new QueryWrapper<AttrGroupEntity>()
    );

    return new PageUtils(page);
  }

  //根据分类Id分页查询所有属性
  @Override
  public PageUtils queryPage(Map<String, Object> params, Integer categoryId) {
    IPage<AttrGroupEntity> page = null;
    QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();
    String key = (String) params.get("key");
    if (!StringUtils.isEmpty(key)) {
      queryWrapper.and((obj) -> {
        obj.eq("attr_group_id", key).or().like("attr_group_name", key);
      });
    }
    if (categoryId == 0) {
      page = this.page(
          new Query<AttrGroupEntity>().getPage(params),
          queryWrapper
      );
    } else {
      queryWrapper.eq("catelog_id", categoryId);
      page = this.page(new Query<AttrGroupEntity>().getPage(params),
          queryWrapper);
    }
    return new PageUtils(page);
  }

  /**
   * 根据分类id查询所有分组和组里面的属性
   */
  @Override
  public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
    //查询当前分类下的所有分组
    List<AttrGroupEntity> attrGroupEntities = this.baseMapper.selectList(
        new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
    //查询每个分组中的属性
    List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(entity -> {
      AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
      BeanUtils.copyProperties(entity, attrGroupWithAttrsVo);
      List<AttrEntity> relationAttr = attrService.getRelationAttr(entity.getAttrGroupId());
      attrGroupWithAttrsVo.setAttrs(relationAttr);
      return attrGroupWithAttrsVo;
    }).collect(Collectors.toList());
    return collect;
  }

}